﻿Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

Public Class FormRk
    Private en As New entity_gjb_ck
    Private dt As DataTable
    Dim objPgdMesGt As New clsPgdMesGt
    Dim objgjbdlinfo As New clsGJBDLInfo
    Dim objGjbMst As New clsGjbMst
    Dim objGJBck As New clsGJBck
    Public usr As String
    Dim httpHelper As New HttpHelper
    Dim warehousecode As String
    Private Sub FormRk_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        warehousecode = GetIniFile("WAREHOUSE", "warehouse_code", "", g_szInitFileName)
        'setMstComboBox(Me.gjb_type, "01")
        rkdt.Text = Now
        setEmptyLOC()
        txtDLNo.Focus()
    End Sub

    Private Sub setEmptyLOC()
        Dim objMstList As New clsMstList
        Dim dt As DataTable
        loc.Items.Clear()
        dt = objMstList.getEmptyLoc("2")

        If dt.Rows.Count > 0 Then
            For i As Integer = 0 To dt.Rows.Count - 1
                loc.Items.Add(dt.Rows(i)("mst_value"))
            Next
        End If
    End Sub

    Private Sub txtDLNo_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtDLNo.KeyPress
        If e.KeyChar = vbCr Then
            btnSearch.PerformClick()
            SendKeys.Send("{Tab}")
        End If
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        If String.IsNullOrEmpty(txtDLNo.Text.Trim()) Then Exit Sub
        Dim dtt As DataTable = objgjbdlinfo.selectUnusedDLForNo(txtDLNo.Text.Trim)
        If dtt.Rows.Count > 0 Then
            Dim dr As DataRow = dtt.Rows(0)
            pgd_id.Text = dr("pgd_id").ToString
            txt_material_code.Text = dr("material_code")
            txt_gjb_dl_info_sys_id.Text = dr("sys_id")
            txt_mes_project_code.Text = "GⅡ型600支座-结构七-S-CL-001" 'dr("mes_project_code")
            txt_mes_project_name.Text = "GⅡ型600支座-结构七-S-CL-001" 'dr("mes_project_name")
            gjb_type.Text = dr("gjb_type").ToString
            txt_exprie_date.Text = dr("exprie_date").ToString()
            'rkdt.Text = Now
            rk_num.Text = dr("layer_num")
        Else
            MsgBox("未检索到未使用状态下的此吊篮号")
            clear()
        End If
    End Sub

    Private Sub btnRk_Click(sender As Object, e As EventArgs) Handles btnRk.Click
        If String.IsNullOrEmpty(txtDLNo.Text.Trim()) Then
            MsgBox("吊篮号不能为空")
            Exit Sub
        End If

        If String.IsNullOrEmpty(gjb_type.Text.Trim()) Then
            MsgBox("骨架板型号不能为空")
            Exit Sub
        End If

        'If chkEmpty(txtBatchNo) = False Then Exit Sub
        'If chkEmpty(pgd_id) = False Then Exit Sub
        'If chkEmpty(gjb_type) = False Then Exit Sub
        'If chkEmpty(rkdt) = False Then Exit Sub
        'If chkEmpty(rk_num) = False Then Exit Sub
        'If chkEmpty(loc) = False Then Exit Sub
        If MsgBox("确认要进行入库吗？", vbYesNo, "提示") = vbYes Then
            Dim dtt As DataTable = objGJBck.getListByDlNo(txtDLNo.Text.Trim(), "0")
            If dtt.Rows.Count = 0 Then
                setEN()
                If objGJBck.intoCK(en) = -2 Then
                    MsgBox("入库失败")
                Else
                    mesbg("0", "", rk_num.Text, txt_material_code.Text.Trim(), txt_mes_project_code.Text.Trim())
                    Dim rnt_int As Integer = mesRk(rk_num.Text, warehousecode, txt_material_code.Text.Trim(), txt_mes_project_code.Text.Trim())
                    clear()
                    If rnt_int = 1 Then
                        MsgBox("入库成功")
                    Else
                        MsgBox("入库成功,MES入库失败")
                    End If
                End If
            Else
                MsgBox("该吊篮号已入库")
            End If
        End If
    End Sub

    Private Sub clear()
        txtDLNo.Text = ""
        txtBatchNo.Text = ""
        pgd_id.Text = ""
        txt_material_code.Text = ""
        txt_gjb_dl_info_sys_id.Text = ""
        gjb_type.Text = ""
        txt_exprie_date.Text = ""
        rkdt.Text = ""
        rk_num.Text = "0"
        loc.Text = ""
        setEmptyLOC()
        txtDLNo.Focus()
    End Sub

    Private Sub setEN()
        With en
            .factory_id = "TS"
            .dl_no = txtDLNo.Text
            .into_num = rk_num.Text
            .gjb_type = gjb_type.Text
            .exprie_date = txt_exprie_date.Text.Trim()
            .gjb_dl_info_sys_id = txt_gjb_dl_info_sys_id.Text.Trim()
            .pgd_id = pgd_id.Text
            .material_code = txt_material_code.Text.Trim()
            .location = loc.Text
            .begin_date = Now
            .status = "0"
            .into_ope = usr
            .update_time = Now
        End With
    End Sub
    Private Function mesbg(gtSysId As String, gtPgdMesSysId As String, oper_num As String, productorname As String, b_gtPgdMesProjectCode As String) As String
        Dim params As String = "{"
        params &= """project_code"":""" & b_gtPgdMesProjectCode & """," 'mes合批号
        params &= """qualifiednumber"":""" & Double.Parse(oper_num) & """," '报工的合格数
        params &= """unqualifiednumber"":""" & 0.00 & """," '报工的不合格数
        params &= """productorname"":""" & productorname & """," '报工产品编码
        params &= """procedurename"":""" & "喷涂" & """," '报工工序
        params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        params &= """unqualifiedReason"":""" & "" & """" '不合格原因
        params &= "}"
        Dim rtn As String = httpHelper.HttpPostWebService("dispatchingOrderBG", params)
        Return rtn
    End Function
    Private Function mesRk(oper_num As String, warehousecode As String, productorname As String, b_gtPgdMesProjectCode As String) As Integer
        Dim rtn_int As Integer = 0
        Dim params As String = "{"
        params &= """project_code"":""" & b_gtPgdMesProjectCode & """," 'mes合批号
        params &= """rkcount"":""" & Double.Parse(oper_num) & """," '入库数量
        params &= """warehousecode"":""" & warehousecode & """," '入库仓库编码
        params &= """productorname"":""" & productorname & """," '产品编码
        params &= """procedurename"":""" & "喷涂" & """," '执行工序
        params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        params &= """departmentid"":""TS1103103""" '部门id
        params &= "}"
        Dim rtn As String = httpHelper.HttpPostWebService("productFinishedToWarehouse", params)
        Dim jo As JObject = JsonConvert.DeserializeObject(rtn)
        Dim code As String = jo("code").ToString() '输出0
        Dim msg As String = jo("msg").ToString() '输出操作成功
        If code.Equals("0") And msg.Equals("操作成功") Then
            rtn_int = 1
        End If
        Return rtn_int
    End Function
End Class


Public Class entity_gjb_ck
    Public factory_id As String
    Public dl_no As String
    Public gjb_dl_info_sys_id As String
    Public into_num As String
    Public gjb_type As String
    Public exprie_date As String
    Public pgd_id As String
    Public material_code As String
    Public batch_no As String
    Public location As String
    Public begin_date As DateTime
    Public status As String
    Public end_date As DateTime
    Public into_ope As String
    Public out_ope As String
    Public update_time As DateTime
End Class